package com.dianrui.firemenmonitor.activity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.widget.ImageView;

import com.dianrui.firemenmonitor.Constant;
import com.dianrui.firemenmonitor.util.GlideUtil;
import com.dianrui.firemenmonitor.util.SPUtils;
import com.dianrui.firemenmonitor.view.Immersion.Eyes;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

public abstract class BaseActivity extends AppCompatActivity {
    public SPUtils spUtils;
    private List<ImageView> imageViewList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);//去除标题栏
        super.onCreate(savedInstanceState);
        setContentView(getContentViewId());
        spUtils = new SPUtils(this, Constant.cfgFileName, Context.MODE_PRIVATE);
        ButterKnife.bind(this);
        if((getIntent().getFlags()& Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)!=0){
            //结束你的activity
            finish();
            return;
        }
        initView();
    }


    protected void JumpActivitys(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }

    protected void JumpKillActivitys(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
        finish();
    }

    public void JumpLoginActivitys(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
    }

    public void customInit(int statusColor) {//自定义初始化方法，1.是否滑动返回，2.指定沉浸式颜色
        if (-1 != statusColor) {
            Eyes.setStatusBarLightMode(BaseActivity.this, ContextCompat.getColor(getBaseContext(), statusColor));
        }
    }

    public abstract int getContentViewId();

    public abstract void initView();

    /**
     * 保存图片引用
     *
     * @param imageView
     */
    public void addImageViewList(ImageView imageView) {
        if (imageViewList != null) {
            imageViewList.add(imageView);
        }
    }


    /**
     * 清除图片引用
     */
    public void clearImageViewList() {
        if (imageViewList != null) {
            for (int i = 0; i < imageViewList.size(); i++) {
                if (imageViewList.get(i) != null) {
                    GlideUtil.clearImg(imageViewList.get(i));
                    imageViewList.set(i, null);
                }
            }
            if (imageViewList != null) {
                imageViewList.clear();
            }
            imageViewList = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        clearImageViewList();
    }
}
